﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace SilverMap.Editor.Converters
{
	public class SizeConverter : IValueConverter
	{
		private Size _currentSize = new Size();

		#region IValueConverter Members

		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			// value == Size value
			// parameter == Target property (Width or Height)
			if (value == null || parameter == null)
				return null;

			if (value.GetType() != typeof(Size))
				return null;

			_currentSize = (Size)value;

			switch (parameter.ToString().ToLower())
			{	
				case "width":
					return _currentSize.Width;
				case "height":
					return _currentSize.Height;
			}

			return null;
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null || parameter == null)
				return null;

			double result;
			Double.TryParse(value.ToString(), out result);

			switch (parameter.ToString().ToLower())
			{	
				case "width":
					_currentSize.Width = result;
					break;
				case "height":
					_currentSize.Height = result;
					break;
			}

			return _currentSize;
		}

		#endregion
	}
}
